import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies  from 'js-cookie'
//1.定义路由组件（即页面）引入两个页面
import Home from '../views/Home.vue'
import User from '../views/User.vue'
import Main from '../views/Main.vue'
import Mall from '../views/Mall.vue'
import PageOne from '../views/PageOne.vue'
import PageTwo from '../views/PageTwo.vue'
import Login from '../views/Login.vue'
import home from '@/api/mockServerData/home'
Vue.use(VueRouter)


//2.定义路由,将组件映射到路由
const routes = [
  //主路由
    {
      path:'/login',
      name:'login',
      component:Login
    },
    {
      path:'/',
      component:Main,
      // name:home,
      redirect:'/home',//重定向
      children:[
        {path:'home',name:'home',component:Home},//首页
        {path:'user',name:'user',component:User},//用户管理
        {path:'mall',name:'mall',component:Mall},//商品管理
        {path:'page1',name:'page1',component:PageOne},//页面1
        {path:'page2',name:'page2',component:PageTwo},//页面2
      ]
    }
  ]
//3.创建router实例
const router = new VueRouter({
    routes // (缩写) 相当于 routes: routes
  })

  //添加全局前置导航守卫
  // router.beforeEach((to, from, next) => {
  //   // 判断token是否存在
  //  const token= Cookies.get('token')
  //  //token不存在，说明未登录，跳转至登陆页
  //  if(!token&&to.name!=='login'){
  //    next({name:'login'})
  //  }
  //  else if(token&&to.name==='login')//token存在，用户登录，跳转到首页
  //  {
  //    next({name:'home'})
  //  }
  //  else{
  //   next();
  //  }
  // })
export default router

//4.创建和挂载根实例,在main.js根节点中,首先进行引入，然后在实例中进行挂载